/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package koncowka;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author root
 */
public class Aktualizator extends Thread{
    private boolean stop;
    private static long WAITE_TIME = 5000;

    public Aktualizator() {
    }

    @Override
    public void run() {
        try {
            super.run();
            this.stop=false;
            synchronized(this){
            this.wait(WAITE_TIME);
            }
            
            while(!stop)
            {
                List<com.Ap> lista = new ArrayList<com.Ap>();
                for (Ap ap : Koncowka.getListaAp()) {
                    com.Ap tmp = new com.Ap();
                    tmp.setBSSID(ap.getBSSID());
                    tmp.setESSID(ap.getESSID());
                    for (Klient kl : ap.getListaKlientow()) {
                        com.Klient klient = new com.Klient();
                        klient.setApBSSID(kl.getApBSSID());
                        klient.setMAC(kl.getMAC());
                        tmp.getListaKlientow().add(klient);
                    }
                    lista.add(tmp);
                }
                wyslijSieci(lista);
                synchronized(this){
                    this.wait(WAITE_TIME);
                }
                
                List<com.Regula> reguly1 = pobierzReguly();
                List<Regula> reguly2 = new ArrayList<Regula>();
                for (com.Regula reguly11 : reguly1) {
                    Regula reg = new Regula(reguly11.getApBSSID(), reguly11.getClientMAC(), reguly11.isCzyBlokowac());
                    reguly2.add(reg);
                }
                Koncowka.setListaRegul(reguly2);
               
                synchronized(this){
                    this.wait(WAITE_TIME);
                }
                
            }
            
        } catch (InterruptedException ex) {
            Logger.getLogger(Aktualizator.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public void koniec()
    {
        this.stop=true;
    }

    
    private static java.util.List<com.Regula> pobierzReguly() {
        com.PolaczenieService service = new com.PolaczenieService();
        com.Serwer port = service.getSerwerPort();
        return port.pobierzReguly();
    }

    private static void wyslijSieci(java.util.List<com.Ap> arg0) {
        
        com.PolaczenieService service = new com.PolaczenieService();
        com.Serwer port = service.getSerwerPort();
         port.wyslijSieci(arg0);
        //System.out.println("Wyslalem sieci Koncowka");
    }
    
}
